package com.zy.pro.letter.web;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.city.model.City;
import com.zy.pro.city.web.form.CitySearchForm;
import com.zy.pro.letter.model.Letter;
import com.zy.pro.letter.service.LetterService;
import com.zy.pro.letter.web.form.LetterSearchForm;
import com.zy.pro.order.model.OrderItem;
import com.zy.pro.order.web.form.OrderItemSearchForm;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.web.form.ShopSearchForm;
/**
 * 站内信管理控制器
 *
 */
@Controller
@RequestMapping("/control/letter/letterController")
public class LetterController extends BaseController {

	private static final Logger logger = Logger.getLogger(LetterController.class);
	
	/**
	 * 站内信管理服务类
	 */
	@Resource(name = "letterServiceImpl")
	private LetterService letterService;
	
	/**
	 * 系统-列出站内信管理
	 */
	@RequestMapping("/sysLetterList")
	public String sysLetterList(LetterSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			if(form.getLetterTitle() != null && form.getLetterTitle() != ""){
				form.setLetterTitle(form.getLetterTitle().trim());
			}
			if(form.getLetterUserName() != null && form.getLetterUserName() != ""){
				form.setLetterUserName(form.getLetterUserName().trim());
			}
			if(form.getLetterShopName() != null && form.getLetterShopName() != ""){
				form.setLetterShopName(form.getLetterShopName().trim());
			}
			if(form.getUser().getUserName() != null && form.getUser().getUserName() != ""){
				form.getUser().setUserName(form.getUser().getUserName().trim());
			}
			if(form.getShop().getShopName() != null && form.getShop().getShopName() != ""){
				form.getShop().setShopName(form.getShop().getShopName().trim());
			}
			
			form.getOrderby().put("letterCreateTime", "desc");
			
			//创建页面对象
			PageView<Letter> pageView = new PageView<Letter>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(letterService.getData(form));
			
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			map.addAttribute("reqUrl", "/control/letter/letterController/sysLetterList.do");
			//TODO 转发页面
			return "/control/system/letter/sysLetterList";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-显示订单详情管理
	 */
	@RequestMapping("/dispalySysDetail")
	public String dispalySysDetail(Letter letter, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			letter = letterService.find(letter.getLetterId());
			map.addAttribute("letter", letter);
			
			//TODO 转发页面
			return "/control/system/letter/sysDetailLetter";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-删除单个订单管理
	 */
	@RequestMapping("/deletedLetter")
	public void deletedLetter(LetterSearchForm form, ModelMap map,HttpServletRequest request,HttpServletResponse response) {
		try {
			//TODO  eg:orderItemService.delete(form.getId());
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			Letter l = letterService.find(form.getLetterId());
			if(l != null){
				Letter letter = new Letter();
				letter.setLetterId(form.getLetterId());
				letter.setLetterStatus(3);//1:正常,2:冻结,3:删除
				letterService.updateBySelective(letter);
				//letterService.delete(form.getLetterId());
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	
	
	/**
	 * 列出站内信管理
	 */
	@RequestMapping("/list")
	public String list(LetterSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<Letter> pageView = new PageView<Letter>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(letterService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示添加站内信管理
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(Letter letter, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 添加站内信管理
	 */
	@RequestMapping("/add")
	public String add(Letter letter, ModelMap map) {
		try {
			//TODO 设置letter  eg:主键信息
			
			letterService.save(letter);
			return list(new LetterSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示编辑站内信管理
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(LetterSearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:Letter letter = letterService.find(form.getId());
			//eg:map.addAttribute("entity", letter);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑站内信管理
	 */
	@RequestMapping("/edit")
	public String edit(Letter Letter, ModelMap map) {
		try {
			//TODO:处理站内信管理
			
			
			letterService.updateBySelective(Letter);
			return list(new LetterSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	/**
	 * 删除单个站内信管理
	 */
	@RequestMapping("/deleteletter")
	public String delete(LetterSearchForm form, ModelMap map) {
		try {
			//TODO  eg:letterService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 批量删除站内信管理
	 */
	@RequestMapping("/batchDelete")
	public String deletes(LetterSearchForm form, ModelMap map) {
		try {
//			TODO  eg:letterService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
}
